/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserController
 * Author:   zhouheng
 * Date:     2019/6/8 9:21
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhouh.golden.web.controller.system;

import com.zhouh.golden.common.domain.ResponseBo;
import com.zhouh.golden.system.domain.MyUser;
import com.zhouh.golden.system.service.UserService;
import com.zhouh.golden.web.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/6/8
 * @since 1.0.0
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @GetMapping("/test")
    public Map test(){
        Map<String,String> map = new HashMap<>();
        map.put("aaa","bbb");

        return map;
    }

    @GetMapping("/hello")
    public Map hello(){
        Map<String,String> map = new HashMap<>();
        map.put("hello","world");

        return map;
    }


    @ResponseBody
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    public ResponseBo regist(@RequestBody MyUser user) {
        try {
            MyUser result = this.userService.findByName(user.getUsername());
            if (result != null) {
                return ResponseBo.warn("该用户名已被使用！");
            }
            result = this.userService.findByMobile(user.getMobile());
            if (result != null) {
                return ResponseBo.warn("该手机号已绑定！");
            }

            user.setPassword(this.passwordEncoder.encode(user.getPassword()));
            this.userService.registUser(user);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("注册失败", e);
            return ResponseBo.error("注册失败，请联系网站管理员！");
        }
    }

}